<?php

//------------------------
// 类型规格控制器
//-------------------------
namespace app\admin\controller;

use app\common\controller\AdminBase;
use app\common\model\TypeSpec as TypeSpecModel;
use app\common\model\Goods as GoodsModel;
// use app\common\model\GoodsTypeSpecView as GoodsTypeSpecViewModel;
use think\Config;
use think\Db;

class TypeSpec extends AdminBase
{
    protected $TypeSpecModel;
    protected $GoodsModel;
    protected $GoodsTypeSpecViewModel;

    protected function _initialize()
    {
        parent::_initialize();
        $this->TypeSpecModel               = new TypeSpecModel();
        $this->GoodsModel                  = new GoodsModel();
        // $this->GoodsTypeSpecViewModel      = new GoodsTypeSpecViewModel();
    }


    // 列表
    public function index(){
        $id = $this->request->param('id',0,'intval');

        $where['type_id'] = array('eq',$id);

        $info = $this->TypeSpecModel->where($where)->order('seq')->select()->toArray();

        for ($i=0; $i < count($info); $i++) { 
            $item = array();
            $item = Db('SpecItem')->where('spec_id',$info[$i]['id'])->select()->toArray();
            $_item = implode(',',two_to_one($item,'item'));
            $info[$i]['item'] = $_item;
        }

        $this->assign('type_id',$id);
        $this->assign('info',$info);

        return $this->fetch('index');
    }


    // 添加页面
    function add(){
        $type_id = $this->request->param('type_id',0,'intval');

        if (!$type_id) {
            $this->error('缺少添加属性的类型type_id');
        }

        $this->assign('type_id',$type_id);
        return $this->fetch('add');
    }

    /**
     * @desc 添加操作
     */
    public function addProc() {
        $data            = $this->request->param();

        $validate_result = $this->validate($data, 'TypeSpec.add');

        if ($validate_result !== true) {
                $this->error($validate_result);
        }

        $this->TypeSpecModel->allowField(true)->save($data);
        $spec_id  = $this->TypeSpecModel->id;

        if ($spec_id === false) {
            $this->error($this->TypeSpecModel->getError());
        } else {
            // 处理规格项
            $item_arr = explode("\r\n", $data['item']);//根据换行拆分成数组
            $item_arr = array_unique($item_arr);//去重
            $itemDate = array();
            foreach ($item_arr as $k => $v) {
                $itemDate[$k]['spec_id'] = $spec_id;
                $itemDate[$k]['item']    = $v;
            }
            Db('specItem')->insertAll($itemDate);

            $this->success('添加成功');
        }
    }


    // 编辑页面
    function edit(){
        $id = $this->request->param('id',0,'intval');

        $data = $this->TypeSpecModel->where('id',$id)->find()->toArray();
        $item = Db('SpecItem')->where('spec_id',$data['id'])->select()->toArray();
        $_item = implode("\r\n",two_to_one($item,'item'));
        $data['item'] = $_item;

        $this->assign('data',$data);
        return $this->fetch('edit');
    }

    // 编辑操作
    function editProc(){
        $data  = $this->request->param('');

        $validate_result = $this->validate($data, 'TypeSpec.edit');

        if ($validate_result !== true) {
                $this->error($validate_result);
        }

        // 处理新旧规格项
        $old_item = Db('SpecItem')->where('spec_id',$data['id'])->select()->toArray();
        $old_item = two_to_one($old_item,'item');

        $new_item = explode("\r\n", $data['item']);//根据换行拆分成数组
        $new_item = array_unique($new_item);//去重
        $new_item = del_arr_value($new_item,'');//删除空


        $del_item = array_diff($old_item,$new_item);// 比较旧新得到需要删除的规格项
        $del_item = implode(',',$del_item);

        if ($del_item) {
            Db('SpecItem')->where('item','in',$del_item)->delete();
        }

        $add_item = array_diff($new_item,$old_item);// 比较新旧得到需要新增的规格项

        $add_item_date = array();
        foreach ($add_item as $k => $v) {
            $add_item_date[$k]['spec_id'] = $data['id'];
            $add_item_date[$k]['item']    = $v;
        }

        if ($add_item_date) {
            Db('SpecItem')->insertAll($add_item_date);
        }


        $res = $this->TypeSpecModel->allowField(true)->save($data,$data['id']);

        if ($res === false) {
            $this->error($this->TypeSpecModel->getError());
        } else {
            $this->success('保存成功');
        }
    }



    // 删除操作
    public function delete(){
        $id = $this->request->param('id',0,'intval');

        $where['id'] = array('eq',$id);

        $res = $this->TypeSpecModel->where($where)->delete();

        if ($res == false) {
            $this->error($this->TypeSpecModel->getError());
        } else{
            // 删除成功后
            Db('SpecItem')->where('spec_id',$id)->delete();

            $this->success('删除成功');
        }
    }






}